package com.example.demo.controller;

import com.example.demo.common.BusinessException;
import com.example.demo.common.ErrorEnum;
import com.example.demo.config.myannotation.code.LogAnno.LogAnno;
import com.example.demo.entity.Position;
import com.example.demo.outstyle.EchartData;
import com.example.demo.outstyle.Message;
import com.example.demo.outstyle.TableData;
import com.example.demo.service.PositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "position")
@Controller
@RequestMapping(value = "/logic/position")
@CrossOrigin
public class PositionController {
    @Autowired
    private PositionService positionServiceImp;

    @ApiOperation(value = "查询全部招聘信息")
    @PostMapping("/search")
    @ResponseBody
    @LogAnno(operatType = "查询全部招聘信息")
    public TableData selectAllInfo(int page, int limit, Position position) {
        List<Position> positions = positionServiceImp.selectAll(page,limit,position);
        TableData msg = new TableData();
        msg.setState(0);
        msg.setMsg("查询成功");
        msg.setCount(positionServiceImp.selectAllCount(page,limit,position));

        msg.setData(positions);
        return msg;
    }
    @ApiOperation(value = "根据id删除招聘信息")
    @DeleteMapping("/delete")
    @ResponseBody
    @LogAnno(operatType = "根据id删除招聘信息")
    public Message deleteInfo(String id) {
        int result =  positionServiceImp.deleteInfo(id);

        Message message = new Message();
        if (result > 0) {
            message.setMsg("成功删除");
            message.setState(0);
        } else {
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        return message;
    }
    @ApiOperation(value = "根据id更新招聘信息")
    @PostMapping("/modify")
    @ResponseBody
    @LogAnno(operatType = "根据id更新招聘信息")
    public Message updateInfo(Position position){
        int res = positionServiceImp.updateInfo(position);
        Message message = new Message();

        if (res > 0) {
            message.setMsg("修改成功");
            message.setState(0);
        } else {
            throw new BusinessException(ErrorEnum.E_xiugaishibai);
        }
        return message;
    }
    @ApiOperation(value = "插入新的招聘信息")
    @PostMapping("/insert")
    @ResponseBody
    @LogAnno(operatType = "插入新的招聘信息")
    public Message insertInfo(Position po) {
        int res = positionServiceImp.insertInfo(po);
        Message message = new Message();
        if (res > 0) {
            message.setState(0);
            message.setMsg("增加成功");
        } else {
            throw new BusinessException(ErrorEnum.E_charushibai);
        }
        return message;
    }
    @ApiOperation(value = "获得图索要的数据")
    @PostMapping("/selectEchar")
    @ResponseBody
    public Message echarGet() {
        Message message = new Message();
        message.setMsg("查询成功");
        message.setState(0);

        EchartData echartData = new EchartData();
        echartData.setBase(positionServiceImp.selectBaseData());
        echartData.setCompanyBySize(positionServiceImp.selectCompanyBySize());
        echartData.setCompanyLogo(positionServiceImp.selectCompanyLogo());
        echartData.setAdvantageWords(positionServiceImp.selectComponyWelfare());
        echartData.setLanguageCounts(positionServiceImp.selectLanguageCounts());
        echartData.setPositionByCity(positionServiceImp.selectPositionByCity());
        echartData.setPositionByEducation(positionServiceImp.selectPositionByEducation());
        echartData.setPositionBySalary(positionServiceImp.selectPositionBySalary());
        echartData.setSalaryByWorkYear(positionServiceImp.selectSalaryByWorkYear());
        echartData.setPositionByLanguage(positionServiceImp.selectPositionByLanguage());
        echartData.setTop5ByDistrict(positionServiceImp.selectTop5ByDistrict());
        echartData.setByFamous(positionServiceImp.selectByFamous());
        message.setData(echartData);

        return message;
    }


    /**
     * 职业类型通过firstType、secondType和thirdType形成了一个树形结构。
     * 返回整个树形结构。
     * @return
     */
    @ApiOperation(value = "获取类型的三级结构")
    @PostMapping("/type")
    @ResponseBody
    @LogAnno(operatType = "获取类型的三级结构")
    public Message getType() {
        Message message = new Message();
        message.setState(0);
        message.setMsg("成功");
        message.setData(positionServiceImp.getType());
        return message;
    }

    /**
     * 返回当前分支下的地区列表
     * @return
     */
    @ApiOperation(value = "获取地区的二级结构")
    @PostMapping("/location")
    @ResponseBody
    @LogAnno(operatType = "获取地区的二级结构")
    public Message getLocation() {
        Message message = new Message();
        message.setState(0);
        message.setMsg("成功");
        message.setData(positionServiceImp.getLocation());
        return message;
    }

    @ApiOperation(value = "根据逻辑删除招聘信息")
    @PostMapping("/status")
    @ResponseBody
    @LogAnno(operatType = "根据id逻辑删除招聘信息")
    public Message deleteByIdLogic(String id) {
        Position position = new Position();
        position.setId(id);
        position.setStatus("0");
        int result =  positionServiceImp.updateInfo(position);

        Message message = new Message();
        if (result > 0) {
            message.setMsg("成功删除");
            message.setState(0);
        } else {
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        return message;
    }
    @ApiOperation(value = "查询企业")
    @PostMapping("/companySearch")
    @ResponseBody
    @LogAnno(operatType = "查询企业")
    public Message selectCompany(String company) {
        List<String> keyWordList = new ArrayList<>();
        if (company != null && company != " "){
            keyWordList = Arrays.asList(company.split(" +"));
        }
        List<String> companys = positionServiceImp.selectCompany(keyWordList);
        Message msg = new Message();
        msg.setState(0);
        msg.setMsg("查询成功");
        msg.setData(companys);
        return msg;
    }
    @ApiOperation(value = "查询职位")
    @PostMapping("/positionSearch")
    @ResponseBody
    @LogAnno(operatType = "查询职位")
    public Message selectPosition(String position) {
        List<String> keyWordList = new ArrayList<>();
        if (position != null && position != " "){
            keyWordList = Arrays.asList(position.split(" +"));
        }
        for (String s: keyWordList) {
            s = s.trim();
        }
        List<String> company = positionServiceImp.selectPosition(keyWordList);
        Message msg = new Message();
        msg.setState(0);
        msg.setMsg("查询成功");
        msg.setData(company);
        return msg;
    }
    @ApiOperation(value = "查询薪资分布")
    @PostMapping("/test")
    @ResponseBody
    @LogAnno(operatType = "查询薪资分布")
    public Message test() {
        List<Integer> result = positionServiceImp.selectPositionBySalaryTest();
        Message msg = new Message();
        msg.setState(0);
        msg.setMsg("查询成功");
        msg.setData(result);
        return msg;
    }
    @ApiOperation(value = "查询薪资分布")
    @PostMapping("/testsql")
    @ResponseBody
    @LogAnno(operatType = "查询薪资分布")
    public Message testsql() {
        List<Integer> result = positionServiceImp.selectPositionBySalary();
        Message msg = new Message();
        msg.setState(0);
        msg.setMsg("查询成功");
        msg.setData(result);
        return msg;
    }
}
